﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OneMake.Model;
using OneMake.DataAccess;

namespace OneMake.Winforms
{
    public partial class uc_report_qw_sale_feature : UserControl, IBaseReportCtrl
    {
        private DataHelper dataHelper = new DataHelper();
        private ReportHelper rh = new ReportHelper();

        public uc_report_qw_sale_feature()
        {
            InitializeComponent();
        }
        
        private void uc_report_yyrb_Load(object sender, EventArgs e)
        {
            dtpBegin.Value = DateTime.Now.AddDays(-7);
            dtpEnd.Value = DateTime.Now;
            LoadFeatures();
            LoadTypes();
            LoadCategorys();
        }

        private void LoadFeatures()
        {
            List<string> features = rh.get_report_qw_sale_feature_hotsale_feature();
            foreach (string feature in features)
            {
                if (!string.IsNullOrEmpty(feature))
                    cbFeatures.Items.Add(feature);
            }
            if (cbFeatures.Items.Count > 0)
                cbFeatures.SelectedIndex = 0;
        }

        private void LoadTypes()
        {
            List<string> features = rh.get_report_qw_sale_feature_type();
            foreach (string type in features)
            {
                if (!string.IsNullOrEmpty(type))
                    cbTypes.Items.Add(type);
            }
            if (cbTypes.Items.Count > 0)
                cbTypes.SelectedIndex = 0;
        }

        private void LoadCategorys()
        {
            List<string> categorys = rh.get_report_qw_sale_feature_category();
            foreach (string category in categorys)
            {
                if (!string.IsNullOrEmpty(category))
                    cbCategorys.Items.Add(category);
            }
            if (cbCategorys.Items.Count > 0)
                cbCategorys.SelectedIndex = 0;
        }

        public string ReportName
        {
            get { return "report_qw_sale_feature"; }
        }

        public DockStyle Dock2 
        {
            get { return this.Dock; }
            set { this.Dock = value; }
        }

        public Control Parent2
        {
            get { return this.Parent; }
            set { this.Parent = value; }
        }

        public void LoadData() 
        {
            lbProgress.Visible = true;
            panel1.Refresh();

            this.dgvReport.DataSource = null;
            this.dgvReport.Refresh();

            string selFeature = cbFeatures.Text;
            string type = cbTypes.Text;
            string category = cbCategorys.Text;

            DateTime selBeginTime = dtpBegin.Value;
            DateTime selEndTime = dtpEnd.Value;

            report_qw_sale_feature report = rh.get_report_report_qw_sale_feature(selFeature, type, category, selBeginTime, selEndTime);
            initColumn(report.titles);
            foreach (KeyValuePair<string, Dictionary<DateTime, double>> row in report.values)
            {
                string key = row.Key;

                int idx = dgvReport.Rows.Add();
                DataGridViewRow gridRow = dgvReport.Rows[idx];
                gridRow.Cells["行标签"].Value = key;
                
                foreach (KeyValuePair<DateTime, double> subRow in row.Value)
                {
                    gridRow.Cells[subRow.Key.ToString("yyyy年MM月")].Value = subRow.Value;
                }
            }

            lbProgress.Visible = false;
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Tools.ExportToExcel.ExportToExcel exp = new Tools.ExportToExcel.ExportToExcel();
            sfdExportToxcel.FileName = "导出文件1.xlsx";
            sfdExportToxcel.Filter = "Excel表格(*.xlsx)|*.xlsx|Todos los Archivos (*.*)|*.*";
            sfdExportToxcel.FilterIndex = 1;
            sfdExportToxcel.RestoreDirectory = true;

            if (sfdExportToxcel.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = sfdExportToxcel.FileName;
                try
                {
                    exp.dataGridView2Excel(this.dgvReport, path);
                    MessageBox.Show("导出完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            } 
        }

        private void initColumn(List<string> titles)
        {
            dgvReport.Columns.Clear();

            foreach (string title in titles)
            {
                DataGridViewColumn col = new DataGridViewTextBoxColumn();
                col.Name = title;
                col.HeaderText = title;
                col.DataPropertyName = title;
                dgvReport.Columns.Add(col);
            }
        }
    }
}
